/* -- Include the precompiled libraries -- */

#include <main.h>
#include <extra/rgView.h>
#include <extra/rgEvents.h>
/*
#ifdef WIN32
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")
#endif
*/
#include "SDL.h"

/*
void DrawPixel(SDL_Surface *screen, Uint8 R, Uint8 G, Uint8 B, Uint32 x, Uint32 y)
{

}
*/

rgReal UnitRand()
{
      return (rgReal)rand() / (RAND_MAX + 1);
}


int main(int argc, char **argv)
{
	argc = 0;
	argv = 0;
	
	rgTransformation c2w;
	//w2c.m_matrix.setIdentity();
	c2w.rotateX(PI/8);
	c2w.traslate(0, 300, -600);
	//
	
	//w2c.setIdentity();
	rgReal nearPlane = 100;
	rgReal farPlane = 1024;
	rgReal fov = 45;
	//rgInt xRes = 1024;rgInt yRes = 768;
	//rgInt xRes = 800;rgInt yRes = 600;
	rgInt xRes = 640;rgInt yRes = 480;
	//rgInt xRes = 400;rgInt yRes = 400*3/4;

	SDL_Init ( SDL_INIT_VIDEO ) ;
	
	rg::camera cam(c2w, nearPlane, farPlane, fov, xRes, yRes);
	rg::view view(xRes, yRes);

	//rgUint32 green = SDL_MapRGB(view.m_pSurface->format, 0, 255, 0);
	//rgUint32 red = SDL_MapRGB(view.m_pSurface->format, 255, 0, 0);

	rg::material matWhite;
	matWhite.m_ambientColor = rg::color::white;
	matWhite.m_ambientIntensity = 0.2;
	matWhite.m_diffuseColor = rg::color::white;
	matWhite.m_diffuseFactor = 0.8;
	//matWhite.m_specularFactor = 0.0f;
	//matWhite.m_specularExponent = 20.0f;
	//matWhite.m_specularColor = color::white;

	rg::material matYellow;
	matYellow.m_ambientColor = rg::color::yellow;
	matYellow.m_ambientIntensity = 0.9;
	matYellow.m_diffuseColor = rg::color::yellow;
	matYellow.m_diffuseFactor = 0.8;
	//matYellow.m_specularFactor = 0.0f;
	//matYellow.m_specularExponent = 20.0f;
	//matYellow.m_specularColor = color::yellow;

	rg::material matRed;
	matRed.m_ambientColor = rg::color::red;
	matRed.m_ambientIntensity = 0.2;
	matRed.m_diffuseColor = rg::color::red;
	matRed.m_diffuseFactor = 0.8;
	//matRed.m_specularFactor = 0.0f;
	//matRed.m_specularExponent = 20.0f;
	//matRed.m_specularColor = color::red;

	rg::material matBlue;
	matBlue.m_ambientColor = rg::color::blue;
	matBlue.m_ambientIntensity = 0.2;
	matBlue.m_diffuseColor = rg::color::blue;
	matBlue.m_diffuseFactor = 0.8;
	//matBlue.m_specularFactor = 0.0f;
	//matBlue.m_specularExponent = 20.0f;
	//matBlue.m_specularColor = color::blue;

	rg::material matGreen;
	matGreen.m_ambientColor = rg::color::green;
	matGreen.m_ambientIntensity = 0.2;
	matGreen.m_diffuseColor = rg::color::green;
	matGreen.m_diffuseFactor = 0.8;
	//matGreen.m_specularFactor = 0.0f;
	//matGreen.m_specularExponent = 20.0f;
	//matGreen.m_specularColor = color::green;

	rg::light li(0, 200, 0, 1.0);

	rg::triangle tri1( rgPoint(-1000, 0, -1000),  rgPoint(-1000, 0, 1000), rgPoint(1000, 0, -1000) );
	tri1.setMaterial(&matWhite);
	rg::triangle tri2( rgPoint(1000, 0, -1000), rgPoint(-1000, 0, 1000), rgPoint(1000, 0, 1000) );
	tri2.setMaterial(&matWhite);

	rg::sphere sph1(-200, 25, 150, 25);
	sph1.setMaterial(&matRed);

	rg::sphere sph2(0, 200, 0, 3);
	sph2.setMaterial(&matYellow);

	rg::sphere sph3(0, 100, -150, 15);
	sph3.setMaterial(&matGreen);

	rg::sphere sph4(-250, 50, 150, 10);
	sph4.setMaterial(&matGreen);

	rg::sphere sph5(250, 30, 150, 15);
	sph5.setMaterial(&matBlue);


	rg::scene scn;

	scn.addLight(&li);

	scn.addTriangle(&tri1);
	scn.addTriangle(&tri2);
/*
	for (unsigned i=0;i<100;i++)
	{
		rg::triangle* tri = new rg::triangle( 
			rgPoint(100, 200*UnitRand(), -200*UnitRand()),  
			rgPoint(-100, 200*UnitRand(), 200*UnitRand()), 
			rgPoint(100, 200*UnitRand(), 200*UnitRand()) );

		tri->setMaterial(&matWhite);
		scn.addTriangle(tri);
		//  TODO:delete these triangles
	}
*/
	scn.addSphere(&sph1);
	scn.addSphere(&sph2);
	scn.addSphere(&sph3);
	scn.addSphere(&sph4);
	scn.addSphere(&sph5);
	

	rg::render myRender(&view, &cam, &scn);
	

	rg::events ev(&view, &cam, &myRender);
	ev.run();

	SDL_Quit( );
	
	return 1;
}
